<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

class AppController extends Controller
{
    function index(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'     => 'min:1'
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        $appResult = DB::table("ap_app")
          ->select("id","name","version_name","description","introduction","icon","color","url","need_verified")
          ->where("is_public",true)->get();

        foreach ($appResult as $key => $value) {
            $value->methods = DB::table("ap_app_method")
               ->select("method_id AS id")
               ->where("app_id",$value->id)->get();
        }

        if(!empty($input["user_id"])){
            $appUserResult = DB::table("ap_app_user")
              ->select("app_id")
              ->where("user_id",$input["user_id"])
              ->where("is_deleted",false)->get();

            $appIds = [];
            foreach ($appUserResult as $key => $value) {
                $appIds[] = $value->app_id;
            }
            foreach ($appResult as $key => $value) {
                $value->is_deleted = in_array($value->id, $appIds)?0:1;
            }
        }

        return $this::jsonResponse(false,$appResult);
    }


    function show($id){
        $appResult = DB::table("ap_app")
          ->select("id","name","version_name","version_code","description","introduction","icon","color","url","need_verified")
          ->where("id",$id)
          ->first();

        $appResult->methods = DB::table("ap_app_method")
          ->select("method_id AS id")
          ->where("app_id",$id)
          ->get();

        return $this::jsonResponse(false,$appResult);
    }

    function userIndex($id){
        $appUserResult = DB::table("ap_app_user")
          ->select("id","user_id","is_inform","created_at")
          ->where("app_id",$id)
          ->where("is_deleted",false)
          ->get();

        return $this::jsonResponse(false,$appUserResult);
    }

    function cron(){
        ini_set('default_socket_timeout', 2);

        $appResult = DB::table("ap_app")
          ->select("id","cron_url","cron_interval","cron_delay")
          ->where("cron_interval","!=",0)
          ->where("is_public",true)
          ->get();

        $minite = floor(time()/60);

        $result = [];
        foreach ($appResult as $value) {
            if($minite%$value->cron_interval == $value->cron_delay){
                $response = @file_get_contents($value->cron_url);
                $result[] = [
                    "id"       => $value->id,
                    "response" => $response
                ];
            }
        }
        
        return $this::jsonResponse(false,$result);
     }

    public function versionNewest($appId,$end)
    {
        $result = DB::table("ap_app_version")
            ->select("id","app_id","end","name","code","is_force","description","url","created_at")
            ->where("app_id",$appId)->where("end",$end)
            ->orderBy("created_at","DESC")->first();
        return $this::jsonResponse(false,empty($result)?(object)[]:$result);
    }

    public function init($appId,$end)
    {
        $defaultReplyOptions = [
            [["content"=>"同意","is_expected"=>1,"is_default"=>1],["content"=>"拒绝","is_default"=>1]],
            [["content"=>"好的","is_expected"=>1],["content"=>"不好"]],
        ];
        $tips = [
            "urgent_tips"=>"紧急通知是指？？"
        ];
        $appColors = DB::table("ap_app")
            ->select("url","color")
            ->where("url","!=","")
            ->where("color","!=","")
            ->get();

        $versionResult = DB::table("ap_app_version")
            ->select("id","app_id","end","name","code","is_force","description","url","created_at")
            ->where("app_id",$appId)->where("end",$end)
            ->orderBy("created_at","DESC")->first();

        $returnData = [
            "newest_version"         => $versionResult,
            "default_reply_options"  => $defaultReplyOptions,
            "tips"                   => $tips,
            "app_colors"             => $appColors
        ];
        return $this::jsonResponse(false,$returnData);
    }
}
